package com.xsir.ditan.miniprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xsir.ditan.admin.entity.User;
import com.xsir.ditan.admin.mapper.UserMapper;
import com.xsir.ditan.admin.vo.OrderVo;
import com.xsir.ditan.miniprogram.common.Result;
import com.xsir.ditan.miniprogram.entity.Goods;
import com.xsir.ditan.miniprogram.entity.GoodsDate;
import com.xsir.ditan.miniprogram.entity.OrderGoods;
import com.xsir.ditan.miniprogram.mapper.GoodsDateMapper;
import com.xsir.ditan.miniprogram.mapper.GoodsMapper;
import com.xsir.ditan.miniprogram.mapper.OrderGoodsMapper;
import com.xsir.ditan.miniprogram.service.IOrderGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xsir.ditan.miniprogram.util.DateUtil;
import com.xsir.ditan.miniprogram.vo.OrderMiniVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xsir
 * @since 2021-02-09
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class OrderGoodsServiceImpl extends ServiceImpl<OrderGoodsMapper, OrderGoods> implements IOrderGoodsService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsDateMapper goodsDateMapper;
    @Autowired
    private OrderGoodsMapper orderGoodsMapper;
    
    @Override
    public Map<String, Object> getList() {
        Map<String,Object> data = new HashMap<>(4);
        List<OrderGoods> orderList = list();
        if(!Objects.isNull(orderList) && !orderList.isEmpty()) {
            List<OrderVo> orderVoList = new ArrayList<>();
            orderList.forEach(order -> {
                OrderVo orderVo = new OrderVo();
                BeanUtils.copyProperties(order, orderVo);
                User user = userMapper.selectById(order.getUserId());
                orderVo.setUserName(user.getNickName());
                orderVoList.add(orderVo);
            });
            data.put("data", orderVoList);
        }
        data.put("code", 0);
        return data;
    }
    
    @Override
    public Result addFromMini(Integer uid, Integer goodsId, List<GoodsDate> dateList) {
        Goods goods = goodsMapper.selectById(goodsId);
        BigDecimal price = goods.getPrice();
        BigDecimal allPrice = price.multiply(new BigDecimal(dateList.size()));
        StringBuffer sb = new StringBuffer();
        dateList.forEach(date -> {
            sb.append(date.getDate()).append("|");
        });
        sb.setLength(sb.length() - 1);
        
        // 生成订单
        OrderGoods orderGoods = new OrderGoods();
        orderGoods.setId(DateUtil.getTimeStamp());
        orderGoods.setDate(DateUtil.now());
        orderGoods.setGoodsId(goodsId);
        orderGoods.setGoodsName(goods.getName());
        orderGoods.setGoodsImage(goods.getImage());
        orderGoods.setUserId(uid);
        orderGoods.setPrice(allPrice);
        orderGoods.setStatus(0);
        orderGoods.setDateList(sb.toString());
        
        // 更新goodsDate
        goodsDateMapper.updateBatch(dateList);
        orderGoodsMapper.add(orderGoods);
        
        return Result.ok(null);
    }
    
    @Override
    public Result listForMini(Integer uid, String status) {
        QueryWrapper<OrderGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", uid);
        if("success".equals(status)) {
            wrapper.eq("status", 1);
        } else if("failure".equals(status)){
            wrapper.eq("status", 0);
        }
        List<OrderGoods> list = list(wrapper);
        List<OrderMiniVo> orderMiniVoList = new ArrayList<>();
        if(list != null && !list.isEmpty()) {
            list.forEach(item -> {
                orderMiniVoList.add(new OrderMiniVo(item));
            });
        }
        return Result.ok(orderMiniVoList);
    }
}
